package com.fresh.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fresh.dao.UserDao;
import com.fresh.dao.UserPermissionDao;
import com.fresh.entity.UserEntity;
import com.fresh.entity.UserPermissionEntity;
import com.fresh.service.UserService;
import com.fresh.utils.PageModel;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserPermissionDao userPermissionDao;

	@Override
	public UserEntity queryObject(String id) {
		return userDao.queryObject(id);
	}

	public boolean updateUser(UserEntity userEntity,String menuIds,String serviceIds ) {
		
		userDao.updateUser(userEntity);
		userPermissionDao.deleteUserPermissio(userEntity.getId());
		
		if(!StringUtils.isEmpty(menuIds)){
			String[]  ids  = menuIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				UserPermissionEntity per  = new UserPermissionEntity();
				per.setId(UUID.randomUUID().toString());
				per.setType(1);
				per.setPermissionId(Integer.parseInt(ids[i]));
				per.setUserId(userEntity.getId());
				per.setDel(0);
				per.setCreateTime(new Date());
				userPermissionDao.save(per);
			}
		}
	  if(!StringUtils.isEmpty(serviceIds)){
		   String[]  ids  = serviceIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				UserPermissionEntity per  = new UserPermissionEntity();
				per.setId(UUID.randomUUID().toString());
				per.setType(0);
				per.setPermissionId(Integer.parseInt(ids[i]));
				per.setUserId(userEntity.getId());
				per.setDel(0);
				per.setCreateTime(new Date());
				userPermissionDao.save(per);
			}
		}
		
		return  true;
	}

	@Override
	public UserEntity queryObject(String name, String pwd) {
		
		HashMap< String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("name", name);
		hashMap.put("pwd", pwd);
		return userDao.queryObject(hashMap);
	}

	@Override
	public boolean save(UserEntity userEntity ,String menuIds,String serviceIds ) {
		String id = UUID.randomUUID().toString();
		userEntity.setId(id);
		
		userDao.save(userEntity);
		if(!StringUtils.isEmpty(menuIds)){
			String[]  ids  = menuIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				UserPermissionEntity per  = new UserPermissionEntity();
				per.setId(UUID.randomUUID().toString());
				per.setType(1);
				per.setPermissionId(Integer.parseInt(ids[i]));
				per.setUserId(id);
				per.setDel(0);
				per.setCreateTime(new Date());
				userPermissionDao.save(per);
			}
		}
	  if(!StringUtils.isEmpty(serviceIds)){
		   String[]  ids  = serviceIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				UserPermissionEntity per  = new UserPermissionEntity();
				per.setId(UUID.randomUUID().toString());
				per.setType(0);
				per.setPermissionId(Integer.parseInt(ids[i]));
				per.setUserId(id);
				per.setDel(0);
				per.setCreateTime(new Date());
				userPermissionDao.save(per);
			}
		}
		
		return true;
	}

	@Override
	public List<UserEntity> queryList(String id) {
		
		return userDao.queryList(id);
	}

	@Override
	public UserEntity findById(String id) {
		
		return userDao.findById(id);
	}

	@Override
	public void deleteUser(String id) {
		userDao.deleteUser(id);
		
	}

	@Override
	public Boolean checkLogin(String name, Integer type) {
		return null;
	}

	@Override
	public UserEntity findByType(String shopId) {
		
		return userDao.findByType(shopId);
	}

	@Override
	public PageModel<UserEntity> queryUserPage(int currentPage,
			int size, String shopId) {
		//得到当前页数据的集合
				List<UserEntity> list = userDao.queryUserPage((currentPage-1)*size, size, shopId);
				
				//封装pm
				PageModel<UserEntity> pm=new PageModel<UserEntity>();
				pm.setList(list);
				pm.setCurrentPage(currentPage);
				pm.setSize(size); 
				
				int count=queryUserCount(shopId);
				pm.setSumCount(count);
				//pm.setSumPage(count%size==0 ? count/size : count/size+1);
				pm.setSumPage(count);
				return pm;
	}
	
	@Override
	public int queryUserCount(String shopId) {
		return userDao.queryUserCount(shopId);
	}

	@Override
	public List<UserEntity> findByName(String name) {
		return userDao.findByName(name);
	}

	@Override
	public Boolean updateUserByUserIdAndPwd(String id,String pwd) {
		
		return userDao.updateUserByUserIdAndPwd(id,pwd);
	}

}